package Model.DAO.uselessDao;
import Model.useless.BookForm;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class BookDao {
    private static InitialContext context = null;
    private DataSource dataSource = null;
    public BookDao()
    {
        try
        {
            if (context == null)
            {
                context = new InitialContext();
            }
            dataSource = (DataSource)context.lookup("java:comp/env/jdbc/mysql");

        } catch (NamingException ex) {
            Logger.getLogger(BookDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public List<BookForm> getAllbook() {
        Connection conn = null;
        List<BookForm> target = new ArrayList<BookForm>();
        try {
            conn = dataSource.getConnection();
            PreparedStatement pstmt = conn.prepareStatement("select * from bookform");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                BookForm temp = new BookForm();
                temp.setId(rs.getInt(6));
                temp.setPrice(rs.getDouble(3));
                temp.setTypeid(rs.getInt(5));
                temp.setPage(rs.getInt(7));
                temp.setISBN(rs.getString(1));
                temp.setIddelete(rs.getInt(4));
                temp.setBookname(rs.getString(8));
                temp.setAuthor(rs.getString(2));
                target.add(temp);
            }
            conn.close();
            return target;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<BookForm> searchBook(String bookname)
    {
        Connection conn = null;
        List<BookForm> target = new ArrayList<BookForm>();
        try{
            conn = dataSource.getConnection();
            PreparedStatement pre = conn.prepareStatement("SELECT * FROM bookform WHERE bookname like?");
            pre.setString(1, "%"+bookname+"%");
            ResultSet rst = pre.executeQuery();
            while (rst.next()){
                BookForm temp = new BookForm();
                temp.setId(rst.getInt(6));
                temp.setPrice(rst.getDouble(3));
                temp.setTypeid(rst.getInt(5));
                temp.setPage(rst.getInt(7));
                temp.setISBN(rst.getString(1));
                temp.setIddelete(rst.getInt(4));
                temp.setBookname(rst.getString(8));
                temp.setAuthor(rst.getString(2));
                target.add(temp);
            }
            conn.close();
            return target;
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            return null;
        }
    }

    public int getCount(){
        int rowCount = 0;
        Connection conn = null;
        try{
            conn = dataSource.getConnection();
            PreparedStatement pre = conn.prepareStatement("SELECT * FROM bookform");
            ResultSet rst = pre.executeQuery();
            while(rst.next()) {
                rowCount++;
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally{
            try {
                conn.close();
                return rowCount;
            } catch (Exception e) {
                return rowCount;
            }
        }
    }

    public boolean insertBook(BookForm book)
    {
        Connection conn = null;
        try
        {
            conn = dataSource.getConnection();
            PreparedStatement pre = conn.prepareStatement("INSERT INTO bookform VALUES(?,?,?,?,?,?,?,?)");
            pre.setString(1, book.getISBN());
            pre.setString(2, book.getAuthor());
            pre.setDouble(3, book.getPrice());
            pre.setInt(4, book.getIddelete());
            pre.setInt(5, book.getTypeid());
            pre.setInt(6,book.getId());
            pre.setInt(7,book.getPage());
            pre.setString(8,book.getBookname());
            pre.executeUpdate();
            conn.close();
            return true;
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            return  false;
        }
    }

}
